package com.naiterui.ehp.bs.doctor.controller;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.domain.Doctor;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.bp.utils.qr.WeiXinEncrypUtil;
import com.naiterui.ehp.bs.doctor.exception.ExceptionCodes;
import com.naiterui.ehp.bs.doctor.service.IDoctorService;
import com.naiterui.ehp.bs.doctor.service.IHospitalService;
import com.naiterui.ehp.bs.doctor.utils.PropertyValueConstants;
import com.naiterui.ehp.bs.doctor.vo.HospitalVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Api(tags = {SwaggerTag.DOCTOR + SwaggerTag.RELATIONSHIP})
@RequestMapping("ad/message")
@Controller
public class AdDoctorPhoneMessageController {

    public static final String DOCTOR_TO_PATIENT_SEND_MODEL_HAVA_HOSPITALNAME = "send.model.have.hospitalName";
    public static final String DOCTOR_TO_PATIENT_SEND_MODEL_NOT_HAVA_HOSPITALNAME = "send.model.not.have.hospitalName";

    private static final Logger LOGGER = LoggerFactory.getLogger(AdDoctorPhoneMessageController.class);

    @Autowired
    private IDoctorService doctorService;
    @Autowired
    private IHospitalService hospitalService;

    /**
     * 获取添加手机联系人短信模板
     * MessageController.getMessageForDoctorId()
     *
     * @param doctorId
     *
     * @return
     *
     * @Author linhf
     * @Date 2016年3月24日
     * @since 1.0.0
     */
    @ApiOperation("获取邀请患者短信模板")
    @RequestMapping(value = "model", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Map<String, String> getMessageForDoctorId(Long doctorId) throws BusinessException {

        LOGGER.info("request check mobil message model start.");
        String content = null;
        Map<String, String> map = new HashMap<>();
        if (null == doctorId) {
            LOGGER.error("request check mobil message model interface;param doctorId is null");
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }

        Doctor doctor = this.doctorService.getDoctorById(doctorId);
        // 判断医生信息是否存在
        if (doctor == null) {
            LOGGER.error("request check mobil message model interface error; check doctor is null,doctorId=" + doctorId);
            throw new BusinessException(ExceptionCodes.DOCTORID_NOT_EXITS);
        }
        String url = this.getSpreadDoctorUrl(doctorId);
        // 如果医生所属医院id值不为空，那么医生所有医院是系统设置医院
        if (null != doctor.getHospitalId()) {
            HospitalVO hospitalVo = this.hospitalService.getHospitalById(doctor.getHospitalId());
            if (null != hospitalVo && StringUtils.isNotEmpty(hospitalVo.getName())) {
                content = String.format(ConfigUtil.getString(DOCTOR_TO_PATIENT_SEND_MODEL_HAVA_HOSPITALNAME), hospitalVo.getName(),
                                        doctor.getName(), url);
            }

        }
        // 如果医生设置系统医院信息空，那么判断医生自定义医院是否为空，不为空取医生自定义医院
        if (StringUtils.isEmpty(content) && StringUtils.isNotEmpty(doctor.getHospitalName())) {
            content = String.format(ConfigUtil.getString(DOCTOR_TO_PATIENT_SEND_MODEL_HAVA_HOSPITALNAME), doctor.getHospitalName(),
                                    doctor.getName(), url);
        }
        // 如果系统医院和自定义医生都为空，那么不发送医院信息
        if (StringUtils.isEmpty(content)) {
            content = String.format(ConfigUtil.getString(DOCTOR_TO_PATIENT_SEND_MODEL_NOT_HAVA_HOSPITALNAME), doctor.getName(), url);
        }

        map.put("messageContent", content);

        return map;
    }

    // 获取医生推广医生二维码
    private String getSpreadDoctorUrl(Long doctorId) {

        String baseDoctorUrl = ConfigUtil.getString("doctor.add.mobil.patient.url");
        return WeiXinEncrypUtil.encryUrl(baseDoctorUrl, doctorId, PropertyValueConstants.APP_DOCTOR_2_PATIENT_TYPE);

    }

}
